########################################################
# Project:    Talking to the Populist Radical Right
# Task:       The script merges the data sets with the
#             scores from the CA for the Dutch parties, 
#             normalizes & annotates them
# Author:     Jan Schwalbach (21/07/2022)
########################################################

# Select legislative period and load respective data

load(file="CA_NL_ALL.Rdata")
load(file="CA_NL_MIG.Rdata")
load(file="CA_NL_ED.Rdata")

load(file="CA_NL_ALL_10.Rdata")
load(file="CA_NL_MIG_10.Rdata")
load(file="CA_NL_ED_10.Rdata")

load(file="CA_NL_ALL_12.Rdata")
load(file="CA_NL_MIG_12.Rdata")
load(file="CA_NL_ED_12.Rdata")

Positions_CA_NL_ALL[,1] <- (Positions_CA_NL_ALL[,1]-min(Positions_CA_NL_ALL[,1]))/(max(Positions_CA_NL_ALL[,1])-min(Positions_CA_NL_ALL[,1]))
Positions_CA_NL_ALL[,4] <- (Positions_CA_NL_ALL[,4]-min(Positions_CA_NL_ALL[,4]))/(max(Positions_CA_NL_ALL[,4])-min(Positions_CA_NL_ALL[,4]))
Positions_CA_NL_ALL[,7] <- (Positions_CA_NL_ALL[,7]-min(Positions_CA_NL_ALL[,7]))/(max(Positions_CA_NL_ALL[,7])-min(Positions_CA_NL_ALL[,7]))

Positions_CA_NL_MIG[,1] <- (Positions_CA_NL_MIG[,1]-min(Positions_CA_NL_MIG[,1]))/(max(Positions_CA_NL_MIG[,1])-min(Positions_CA_NL_MIG[,1]))
Positions_CA_NL_MIG[,4] <- (Positions_CA_NL_MIG[,4]-min(Positions_CA_NL_MIG[,4]))/(max(Positions_CA_NL_MIG[,4])-min(Positions_CA_NL_MIG[,4]))
Positions_CA_NL_MIG[,7] <- (Positions_CA_NL_MIG[,7]-min(Positions_CA_NL_MIG[,7]))/(max(Positions_CA_NL_MIG[,7])-min(Positions_CA_NL_MIG[,7]))

Positions_CA_NL_ED[,1] <- (Positions_CA_NL_ED[,1]-min(Positions_CA_NL_ED[,1]))/(max(Positions_CA_NL_ED[,1])-min(Positions_CA_NL_ED[,1]))
Positions_CA_NL_ED[,4] <- (Positions_CA_NL_ED[,4]-min(Positions_CA_NL_ED[,4]))/(max(Positions_CA_NL_ED[,4])-min(Positions_CA_NL_ED[,4]))
Positions_CA_NL_ED[,7] <- (Positions_CA_NL_ED[,7]-min(Positions_CA_NL_ED[,7]))/(max(Positions_CA_NL_ED[,7])-min(Positions_CA_NL_ED[,7]))

CA_NL_ALL_ALL <- Positions_CA_NL_ALL[,1:3]
CA_NL_ALL_GOV <- Positions_CA_NL_ALL[,4:6]
CA_NL_ALL_OPP <- Positions_CA_NL_ALL[,7:9]

CA_NL_MIG_ALL <- Positions_CA_NL_MIG[,1:3]
CA_NL_MIG_GOV <- Positions_CA_NL_MIG[,4:6]
CA_NL_MIG_OPP <- Positions_CA_NL_MIG[,7:9]

CA_NL_ED_ALL <- Positions_CA_NL_ED[,1:3]
CA_NL_ED_GOV <- Positions_CA_NL_ED[,4:6]
CA_NL_ED_OPP <- Positions_CA_NL_ED[,7:9]

names(CA_NL_ALL_ALL)[1] <- "Position"
names(CA_NL_ALL_GOV)[1] <- "Position"
names(CA_NL_ALL_OPP)[1] <- "Position"
names(CA_NL_MIG_ALL)[1] <- "Position"
names(CA_NL_MIG_GOV)[1] <- "Position"
names(CA_NL_MIG_OPP)[1] <- "Position"
names(CA_NL_ED_ALL)[1] <- "Position" 
names(CA_NL_ED_GOV)[1] <- "Position" 
names(CA_NL_ED_OPP)[1] <- "Position" 

names(CA_NL_ALL_ALL)[2] <- "Position2"
names(CA_NL_ALL_GOV)[2] <- "Position2"
names(CA_NL_ALL_OPP)[2] <- "Position2"
names(CA_NL_MIG_ALL)[2] <- "Position2"
names(CA_NL_MIG_GOV)[2] <- "Position2"
names(CA_NL_MIG_OPP)[2] <- "Position2"
names(CA_NL_ED_ALL)[2] <- "Position2" 
names(CA_NL_ED_GOV)[2] <- "Position2" 
names(CA_NL_ED_OPP)[2] <- "Position2" 

# Calculating the difference to the challenger party

CA_NL_ALL_ALL$challenger <- CA_NL_ALL_ALL$Position[6]
CA_NL_ALL_ALL$Difference <- CA_NL_ALL_ALL$Position-CA_NL_ALL_ALL$challenger
CA_NL_ALL_ALL$Difference <- abs(CA_NL_ALL_ALL$Difference)

CA_NL_ALL_GOV$challenger <- CA_NL_ALL_GOV$Position[6]
CA_NL_ALL_GOV$Difference <- CA_NL_ALL_GOV$Position-CA_NL_ALL_GOV$challenger
CA_NL_ALL_GOV$Difference <- abs(CA_NL_ALL_GOV$Difference)

CA_NL_ALL_OPP$challenger <- CA_NL_ALL_OPP$Position[6]
CA_NL_ALL_OPP$Difference <- CA_NL_ALL_OPP$Position-CA_NL_ALL_OPP$challenger
CA_NL_ALL_OPP$Difference <- abs(CA_NL_ALL_OPP$Difference)

CA_NL_MIG_ALL$challenger <- CA_NL_MIG_ALL$Position[6]
CA_NL_MIG_ALL$Difference <- CA_NL_MIG_ALL$Position-CA_NL_MIG_ALL$challenger
CA_NL_MIG_ALL$Difference <- abs(CA_NL_MIG_ALL$Difference)

CA_NL_MIG_GOV$challenger <- CA_NL_MIG_GOV$Position[6]
CA_NL_MIG_GOV$Difference <- CA_NL_MIG_GOV$Position-CA_NL_MIG_GOV$challenger
CA_NL_MIG_GOV$Difference <- abs(CA_NL_MIG_GOV$Difference)

CA_NL_MIG_OPP$challenger <- CA_NL_MIG_OPP$Position[6]
CA_NL_MIG_OPP$Difference <- CA_NL_MIG_OPP$Position-CA_NL_MIG_OPP$challenger
CA_NL_MIG_OPP$Difference <- abs(CA_NL_MIG_OPP$Difference)

CA_NL_ED_ALL$challenger <- CA_NL_ED_ALL$Position[6]
CA_NL_ED_ALL$Difference <- CA_NL_ED_ALL$Position-CA_NL_ED_ALL$challenger
CA_NL_ED_ALL$Difference <- abs(CA_NL_ED_ALL$Difference)

CA_NL_ED_GOV$challenger <- CA_NL_ED_GOV$Position[6]
CA_NL_ED_GOV$Difference <- CA_NL_ED_GOV$Position-CA_NL_ED_GOV$challenger
CA_NL_ED_GOV$Difference <- abs(CA_NL_ED_GOV$Difference)

CA_NL_ED_OPP$challenger <- CA_NL_ED_OPP$Position[6]
CA_NL_ED_OPP$Difference <- CA_NL_ED_OPP$Position-CA_NL_ED_OPP$challenger
CA_NL_ED_OPP$Difference <- abs(CA_NL_ED_OPP$Difference)

# Annotating government/opposition debates

CA_NL_ALL_ALL$type <-"all"
CA_NL_ALL_GOV$type <-"government"  
CA_NL_ALL_OPP$type <-"opposition"
CA_NL_MIG_ALL$type <-"all"
CA_NL_MIG_GOV$type <-"government"  
CA_NL_MIG_OPP$type <-"opposition"
CA_NL_ED_ALL$type <-"all" 
CA_NL_ED_GOV$type <-"government"   
CA_NL_ED_OPP$type <-"opposition" 

CA_NL_ALL_ALL$matter <-"all"
CA_NL_ALL_GOV$matter <-"all"  
CA_NL_ALL_OPP$matter <-"all"
CA_NL_MIG_ALL$matter <-"Migration"
CA_NL_MIG_GOV$matter <-"Migration"  
CA_NL_MIG_OPP$matter <-"Migration"
CA_NL_ED_ALL$matter <-"Education" 
CA_NL_ED_GOV$matter <-"Education"   
CA_NL_ED_OPP$matter <-"Education" 


Positions_NL <- rbind(CA_NL_ALL_ALL,CA_NL_ALL_GOV,CA_NL_ALL_OPP,CA_NL_MIG_ALL,CA_NL_MIG_GOV,CA_NL_MIG_OPP,CA_NL_ED_ALL,CA_NL_ED_GOV,CA_NL_ED_OPP)
Positions_NL$Party <- Positions_NL$party
Positions_NL$country <- "NL"

# left/right parties

Positions_NL$left <- 0
Positions_NL$left[Positions_NL$Party == "D66"] <- 1
Positions_NL$left[Positions_NL$Party == "GL"] <- 1
Positions_NL$left[Positions_NL$Party == "PvdA"] <- 1
Positions_NL$left[Positions_NL$Party == "SP"] <- 1

# mainstream parties

Positions_NL$mainstream <- 0
Positions_NL$mainstream[Positions_NL$Party == "VVD"] <- 1
Positions_NL$mainstream[Positions_NL$Party == "PvdA"] <- 1
Positions_NL$mainstream[Positions_NL$Party == "CDA"] <- 1

# government/opposition parties and legislative period

Positions_NL$year <- 2006
Positions_NL$year <- 2010
Positions_NL$year <- 2012

Positions_NL$Government <- "Opposition"
Positions_NL$Government[Positions_NL$Party == "CDA" & Positions_NL$year == 2006] <- "Government"
Positions_NL$Government[Positions_NL$Party == "CU" & Positions_NL$year == 2006] <- "Government"
Positions_NL$Government[Positions_NL$Party == "PvdA" & Positions_NL$year == 2006] <- "Government"
Positions_NL$Government[Positions_NL$Party == "CDA" & Positions_NL$year == 2010] <- "Government"
Positions_NL$Government[Positions_NL$Party == "VVD" & Positions_NL$year == 2010] <- "Government"
Positions_NL$Government[Positions_NL$Party == "PvdA" & Positions_NL$year == 2012] <- "Government"
Positions_NL$Government[Positions_NL$Party == "VVD" & Positions_NL$year == 2012] <- "Government"

save(Positions_NL, file = "Positions_NL_CA_normal.Rdata") 
save(Positions_NL, file = "Positions_NL_CA_10_normal.Rdata") 
save(Positions_NL, file = "Positions_NL_CA_12_normal.Rdata") 
